///NETWORK COMUNICATION
#define SIZE_OF_INT8					1
#define SIZE_OF_INT16					2
#define SIZE_OF_INT32					4
#define SIZE_OF_FLOAT					4

#define BUFALLOCATE(size)		CByteBuffer::allocate(size)
#define BUFSWAP(bytes,size)		CByteBuffer::swap(bytes,size)
#define BUFBINARY(buf)			buf->getBinary()
#define BUFSIZE(buf)			buf->getSize()
			
#define RINT8(buf)				CByteBuffer::readInt8(buf)
#define RINT16(buf)				CByteBuffer::readInt16(buf)
#define RINT32(buf)				CByteBuffer::readInt32(buf)
#define RBIN(buf,len)			CByteBuffer::readBin(buf,len)
#define RBBIN(buf)				CByteBuffer::readBBin(buf);
#define RSBIN(buf)				CByteBuffer::readSBin(buf);
#define RBSTR(buf)				CByteBuffer::readBString(buf);
#define RSSTR(buf)				CByteBuffer::readSString(buf);

#define PUTINT8(buf,value)		CByteBuffer::writeInt8(value,buf)
#define PUTINT16(buf,value)		CByteBuffer::writeInt16(value,buf)
#define PUTINT32(buf,value)		CByteBuffer::writeInt32(value,buf)
#define PUTBIN(buf,bytes)		CByteBuffer::writeBin(bytes,buf)
#define PUTBBIN(buf,bytes)		CByteBuffer::writeBBin(bytes,buf)
#define PUTSBIN(buf,bytes)		CByteBuffer::writeSBin(bytes,buf)

#define MD5(str)				MD5String(str)
#define MD5BYTES(cbytes, len)   MD5Bytes(cbytes, len)
///
#define ORIENTATION_INVALID				(-1)
#define ORIENTATION_PORTRAIT			1//(1 << 0)
#define ORIENTATION_LANDSCAPE_90		2//(1 << 1)
#define ORIENTATION_LANDSCAPE_270		4//(1 << 2)

//Touch State
#define TOUCH_UP					1//(1<<0)
#define TOUCH_DOWN					2//(1<<1)
#define TOUCH_DRAG					4//(1<<2)

#define MAX_TOUCHES					10
//ResourceType
#define RESTYPE_GFX					0
#define RESTYPE_SFX					1
#define RESTYPE_MUSIC				2
#define RESTYPE_DESIGNER			3
#define RESTYPE_TEXT_PACK			4
//resource attribute
#define RESATT_FIRST_PALLETE		1
#define RESATT_TEXTURE				(1<<8) //first byte use to hold a number
#define RESATT_LINEAR				(1<<9)
#define RESATT_TRUECOLOR			(1<<10)
//Key
#define KEY_HOME				0
#define KEY_BACK				1
#define KEY_SEARCH				2
#define KEY_POWER				3

#define KEY_GAME_G				4
#define KEY_GAME_V				5
#define KEY_GAME_N				6
#define KEY_NARROW_UP			7
#define KEY_NARROW_LEFT			8
#define KEY_NARROW_RIGHT		9
#define KEY_NARROW_DOWN			10

//key state
#define KEY_STATE_RELEASE		1//(1<<0)
#define KEY_STATE_PRESS			2//(1<<1)

//event id of class system
#define EVENT_KEY_PRESS					1
#define EVENT_KEY_RELEASE				2
#define EVENT_HTTP_RECEIVE				3
#define EVENT_SOCIAL_HTTP_RECEIVE		4
#define EVENT_TEXT_INPUT_RECEIVE		5
#define EVENT_TIMER_TICK				6
#define EVENT_CSOCIAL_TOURNAMENT_REWARD	7
#define EVENT_FACEBOOK_LOGIN_OK	        8
#define EVENT_FACEBOOK_LOGIN_CANCEL		9
#define EVENT_FACEBOOK_ACCOUNT_UPDATE   10

//event code of class system
#define ECODE_KEY_PRESS					0x40000001
#define ECODE_KEY_RELEASE				0x40000002
#define ECODE_HTTP_RECEIVE				0x40000003
#define ECODE_SOCIAL_HTTP_RECEIVE		0x40000004
#define ECODE_TEXT_INPUT_RECEIVE		0x40000005
#define ECODE_TIMER_TICK				0x40000006
#define ECODE_CSOCIAL_TOURNAMENT_REWARD	0x40000007
#define ECODE_FACEBOOK_LOGIN_OK			0x40000008
#define ECODE_FACEBOOK_LOGIN_CANCEL		0x40000009
#define ECODE_FACEBOOK_ACCOUNT_UPDATE   0x4000000A

//event id of class engine
#define EVENT_SUBMIT_ACHI			1
#define EVENT_SUBMIT_HIGHSCORE		2
#define EVENT_SHOW_TEXT_INPUT		3
#define EVENT_FACEBOOK_LOGIN		4
#define EVENT_FACEBOOK_AUTHORIZE	5
#define EVENT_FACEBOOK_LOGOUT       6
//event code of class engine
#define ECODE_SUBMIT_ACHI			0xC0000001
#define ECODE_SUBMIT_HIGHSCORE		0xC0000002
#define ECODE_TEXT_INPUT_SHOW		0xC0000003
#define ECODE_FACEBOOK_LOGIN		0xC0000004
#define ECODE_FACEBOOK_AUTHORIZE	0xC0000005
#define ECODE_FACEBOOK_LOGOUT       0xC0000006

//gl version
#define GLESVER_1					1
#define GLESVER_2					2
#define USEGLESVER_1				(GLESVER == GLESVER_1)
#define USEGLESVER_2				(GLESVER == GLESVER_2)

///core graphic 2D
#define CG2DAREA_TYPE_RECT		0
#define CG2DAREA_TYPE_CIRCLE	1
#define CG2DAREA_TYPE_POLYGON	2
///Tester
#define TLOG_NORMAL 0
#define TLOG_TITLE	1
#define TLOG_ERROR	2
#if defined(USE_TESTER)
#   define TLOG Ter->TLog
#else
#   define TLOG(...)
#endif
///
///define type using for engine
///
typedef unsigned char			UINT8;
typedef signed char				INT8;
#if defined(MAC_OS) || defined(ANDROID_OS)
	typedef unsigned char		BYTE;
#endif
typedef unsigned short			UINT16;
typedef signed short			INT16;
typedef unsigned int			UINT32;
typedef signed int				INT32;
typedef unsigned short			UNICHAR;
typedef unsigned long long 		UINT64;
typedef int						HTIMER,HCONNECT,HPAGE;

#if !defined(MAC_OS)
#define THREADHANDLE            HANDLE
#else
#define  THREADHANDLE           pthread_t
#endif

#if defined(MAC_OS)
typedef int SOCKET;
#define INVALID_SOCKET  -1
#endif



///
///define macro using for engine
///
#ifdef MAC_OS
	#define NULL				0
#endif

#define SAFE_DEL(a)					{if(a){delete (a);(a) = NULL;}}
#define SAFE_DEL_ARRAY(a)			{if(a){delete[] (a);(a) = NULL;}}
#define SAFE_DEL_ARRAY_TYPE(a, t)	{if((t)a){delete[] ((t)(a));(a) = NULL;}}
#define SAFE_DEL_ARRAY_OBJ(p, n)	{if (p) {for (int __i = 0; __i < (n); __i++) SAFE_DEL((p)[__i]); SAFE_DEL_ARRAY(p);}}
#define SAFE_DEL_ARRAY_ARRAY(p, n)	{if (p) {for (int __i = 0; __i < (n); __i++) SAFE_DEL_ARRAY((p)[__i]); SAFE_DEL_ARRAY(p);}}
//marco for event
//memcopy (void* src, void* dst, int len)
#define MEMCOPY(src, dst, len) { \
	int l = len; \
	char* p = (char*)src; \
	char* q = (char*)dst; \
	while(l-->0) \
	{*q++ = *p++; \
	} \
}
#define MEMSET(src,c, len) { \
    int l = len;\
    unsigned char* p=(unsigned char*)src; \
    while(l--)*p++ = (unsigned char)c; \
}
#define CTOI(c) (c-'0')
#define STR_TO_UINT(str,len,i) {i=0; for(int j = 0; j< len;j++){i+=( CTOI(str[j])*CMath::POW(10,len-j-1));};}
#define STR_TO_INT(str,len,i) {i=0; int pos = 0; int l = len; bool minus = false;if(str[0]=='-'){minus = true;pos++;};for(int j = pos; j< l;j++){i+=( CTOI(str[j])*CMath::POW(10,l-j-1));};if(minus){i=0-i;};}
#define CHECK_POINT_IN_CIRCLE(X, Y, CX, CY, RAD) (((X-CX)*(X-CX) + (Y-CY)*(Y-CY)) <(RAD*RAD))
#define CHECK_POINT_IN_RECT(X1,Y1,X,Y,W,H) ((X1)>= (X) && (X1) <= ((X)+(W)) && (Y1)>=(Y) && (Y1) <= ((Y)+(H)))

//convert
#define ENCODE_INT16(value) ( ( (value)&0xFF00 | ((value)&0xFF) ) &0xFFFF )
#define DECODE_INT16(first,second) (( ((first)&0xFF) | (((second)<<8)&0xFF00) )&0xFFFF)

//rect stream
#define RECTSTREAM_INIT(id, x, y , w, h) ENCODE_INT16(id), ENCODE_INT16(4), x, x + w, x + w, x, y, y, y + h, y + h
//bit
#define BITON(value,bitflag) {value|=bitflag;}
#define BITOFF(value,bitfag) {value = ((value ^ bitfag) & value);}
#define ISBITON(value,bit) ((value&bit) != 0)
///
///define for Qt
///
#ifdef QT_CREATOR
#define RES_FOLDER "/opt/nokiaqt/res/bin/"
#endif